package me.phelps.webrtctest;

import android.os.Environment;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by User on 2016/11/4.
 */

public class FileHelper {

    public static FileHelper instance = new FileHelper();
    private File file1;
    private File file2;
    private File file3;
    private File file4;
    private DataOutputStream dos1;
    private DataOutputStream dos2;
    private DataOutputStream dos3;
    private DataOutputStream dos4;

    public void init(){
        file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tbslog/testcodec1.pcm");
        file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tbslog/testcodec2.aac");
        file3 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tbslog/testcodec3.pcm");
        file4 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tbslog/testcodec4.pcm");
        checkFile(file1);
        checkFile(file2);
        checkFile(file3);
        checkFile(file4);
        try {
            dos1 = new DataOutputStream(new FileOutputStream(file1));
            dos2 = new DataOutputStream(new FileOutputStream(file2));
            dos3 = new DataOutputStream(new FileOutputStream(file3));
            dos4 = new DataOutputStream(new FileOutputStream(file4));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void checkFile(File file){
        if(file.exists()){
            file.delete();
        }
    }

    public void write1(byte[] data){
        try {
            dos1.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write2(byte[] data){
        try {
            dos2.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write3(byte[] data){
        try {
            dos3.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write4(byte[] data){
        try {
            dos4.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close(){
        try {
            dos1.close();
            dos2.close();
            dos3.close();
            dos4.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
